
package cn.droidlover.xrecyclerview;

import cn.droidlover.xrecyclerview.utils.LogUtils;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.app.Context;

/**
 * XRecyclerContentLayout 自定义组件
 *
 * @author name
 * @since 2021-04-08
 */
public class XRecyclerContentLayout extends DirectionalLayout {

    private DependentLayout contentView;
    private DependentLayout loadingView;
    private DependentLayout errorView;
    private DependentLayout emptyView;

    public XRecyclerContentLayout(Context context) {
        this(context, null);
    }

    public XRecyclerContentLayout(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public XRecyclerContentLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        setupComponent(context);
    }

    /**
     * 绑定布局  默认显示Content
     *
     * @param context 上下文
     */
    private void setupComponent(Context context) {
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_x_recycler_layout, this, true);
        contentView = (DependentLayout) findComponentById(ResourceTable.Id_list_layout);
        loadingView = (DependentLayout) findComponentById(ResourceTable.Id_loading_layout);
        errorView = (DependentLayout) findComponentById(ResourceTable.Id_error_layout);
        emptyView = (DependentLayout) findComponentById(ResourceTable.Id_empty_layout);
        LogUtils.info("XRecyclerView", "==》》setupComponent()");
        showContent();
    }

    public DependentLayout getContentView() {
        return contentView;
    }

    public void setContentView(DependentLayout contentView) {
        this.contentView = contentView;
    }

    public DependentLayout getLoadingView() {
        return loadingView;
    }

    public void setLoadingView(DependentLayout loadingView) {
        this.loadingView = loadingView;
    }

    public DependentLayout getErrorView() {
        return errorView;
    }

    public void setErrorView(DependentLayout errorView) {
        this.errorView = errorView;
    }

    public DependentLayout getEmptyView() {
        return emptyView;
    }

    public void setEmptyView(DependentLayout emptyView) {
        this.emptyView = emptyView;
    }

    /**
     * 添加ContentView组件
     *
     * @param component 自定义组件
     */
    public void addContentView(Component component) {
        if (component != null) {
            contentView.addComponent(component);
        }
    }

    /**
     * removeContentView组件
     *
     * @param component 自定义组件
     */

    public void removeContentView(Component component) {
        if (component != null) {
            contentView.removeComponent(component);
        }
    }

    /**
     * 展示Content
     */

    public void showContent() {
        if (contentView.getVisibility() == Component.VISIBLE) {
            return;
        }
        contentView.setVisibility(Component.VISIBLE);
        loadingView.setVisibility(Component.HIDE);
        errorView.setVisibility(Component.HIDE);
        emptyView.setVisibility(Component.HIDE);
    }

    /**
     * 添加Loading组件
     *
     * @param component 自定义组件
     */
    public void addLoadingView(Component component) {
        if (component != null) {
            loadingView.addComponent(component);
        }
    }

    /**
     * removeLoadingView组件
     *
     * @param component 自定义组件
     */
    public void removeLoadingView(Component component) {
        if (component != null) {
            loadingView.removeComponent(component);
        }
    }

    /**
     * 添加ErrorView组件
     *
     * @param component 自定义组件
     */

    public void addErrorView(Component component) {
        if (component != null) {
            errorView.addComponent(component);
        }
    }

    /**
     * removeErrorView组件
     *
     * @param component 自定义组件
     */

    public void removeErrorView(Component component) {
        if (component != null) {
            errorView.removeComponent(component);
        }
    }

    /**
     * 添加EmptyView组件
     *
     * @param component 自定义组件
     */

    public void addEmptyView(Component component) {
        if (component != null) {
            emptyView.addComponent(component);
        }
    }

    /**
     * removeEmptyView组件
     *
     * @param component 自定义组件
     */

    public void removeEmptyView(Component component) {
        if (component != null) {
            emptyView.removeComponent(component);
        }
    }

    /**
     * 展示Error
     */
    public void showErrorView() {
        if (errorView.getVisibility() == Component.VISIBLE) {
            return;
        }
        contentView.setVisibility(Component.HIDE);
        loadingView.setVisibility(Component.HIDE);
        errorView.setVisibility(Component.VISIBLE);
        emptyView.setVisibility(Component.HIDE);
    }

    /**
     * 展示LoadingView
     */
    public void showLoadingView() {
        if (loadingView.getVisibility() == Component.VISIBLE) {
            return;
        }
        contentView.setVisibility(Component.HIDE);
        loadingView.setVisibility(Component.VISIBLE);
        errorView.setVisibility(Component.HIDE);
        emptyView.setVisibility(Component.HIDE);
    }

    /**
     * 展示EmptyView
     */
    public void showEmptyView() {
        if (emptyView.getVisibility() == Component.VISIBLE) {
            return;
        }
        contentView.setVisibility(Component.HIDE);
        loadingView.setVisibility(Component.HIDE);
        errorView.setVisibility(Component.HIDE);
        emptyView.setVisibility(Component.VISIBLE);
    }
}